package com.fb;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.provider.MediaStore;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.dev.MainActivity;
import com.dev.TextButton;
import com.fb.FileBrowserPage;
import com.log;
import java.io.File;
import java.util.ArrayList;
public class ConfirmDeleteFile extends LinearLayout implements OnClickListener{
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	public File destination;
	TextView title;
	TextView file;
	LinearLayout bottombar;
	TextButton tbConfirm;
	TextButton tbCancel;
	LayoutParams tblp=new LayoutParams(0,-1,1);
	FileBrowserPage fbp;
	MainActivity ma;
	public ConfirmDeleteFile(MainActivity ma)	{
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setWillNotDraw(false);
		title=new TextView(ma){
			@Override
			public void onDraw(Canvas c){
				super.onDraw(c);
				Paint p=new Paint();
				p.setColor(FileBrowserPage.borderColor);
				c.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
			}
		};
		title.setText("是否删除：");
		title.setTextColor(0xff000000);
		title.setPadding(9,0,0,0);
		title.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
		file=new TextView(ma);
		file.setTypeface(Typeface.MONOSPACE);
		file.setPadding(9,0,9,0);
		file.setTextSize(TypedValue.COMPLEX_UNIT_DIP,12);
		file.setTextColor(0xff0000ff);
		tbConfirm=new TextButton(ma,"删除");
		tbConfirm.setOnClickListener(this);
		tbConfirm.borderColor=FileBrowserPage.borderColor;
		tbConfirm.borderWidth=FileBrowserPage.borderWidth;
		tbConfirm.borderTop=true;
		tbCancel=new TextButton(ma,"取消");
		tbCancel.setOnClickListener(this);
		tbCancel.borderColor=FileBrowserPage.borderColor;
		tbCancel.borderWidth=FileBrowserPage.borderWidth;
		tbCancel.borderTop=true;
		bottombar=new LinearLayout(ma);
		bottombar.setOrientation(HORIZONTAL);
		bottombar.addView(tbCancel,tblp);
		bottombar.addView(tbConfirm,tblp);
		addView(title,-1,(int)(30*ma.dp));
		addView(file,-1,-2);
		addView(bottombar,-1,(int)(30*ma.dp));
	}
	public void show(FileBrowserPage fbp){
		this.fbp=fbp;
		if(destination==null){
			int count=0;
			for(int i=0;i<fbp.selected.length;i++){
				if(fbp.selected[i]){
					count++;
				}
			}
			String s1="删除选中的"+count+"项目";
			file.setText(s1);
			wlp.width=ma.gui.getWidth()*2/3;
			wlp.height=-2;
			ma.wm.addView(this,wlp);
		}else{
			String s1=destination.getAbsolutePath()+"\n";
			if(destination.isDirectory()){
				if(destination.canRead()){
					File[] list=destination.listFiles();
					if(list.length>0){
						String s2="注意此目录不为空！";
						SpannableString ss=new SpannableString(s1+s2);
						ss.setSpan(new ForegroundColorSpan(0xff7f0000),s1.length(),ss.length(),0);
						file.setText(ss);
						wlp.width=ma.gui.getWidth()*2/3;
						wlp.height=-2;
						ma.wm.addView(this,wlp);
					}else{
						file.setText(s1);
						wlp.width=ma.gui.getWidth()*2/3;
						wlp.height=-2;
						ma.wm.addView(this,wlp);
					}
				}else{
					String s2="此目录不可读！";
					SpannableString ss=new SpannableString(s1+s2);
					ss.setSpan(new ForegroundColorSpan(0xff7f0000),s1.length(),ss.length(),0);
					file.setText(ss);
					wlp.width=ma.gui.getWidth()*2/3;
					wlp.height=-2;
					ma.wm.addView(this,wlp);
				}
			}else{
				file.setText(s1);
				wlp.width=ma.gui.getWidth()*2/3;
				wlp.height=-2;
				ma.wm.addView(this,wlp);
			}
		}
	}
	void dismiss(){
		ma.wm.removeView(this);
	}
	@Override
	public void onClick(View v){
		if(v==tbCancel){
			dismiss();
		}else if(v==tbConfirm){
			if(destination==null){
				undeleted=0;
				if(fbp.dyn==10){
					ContentResolver mContentResolver=ma.getContentResolver();
					Cursor c;// = mContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
					//int indexData = c.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
					for(int i=0;i<fbp.selected.length;i++){
						if(fbp.selected[i]){
							String path=fbp.willshow[i].getAbsolutePath();
							if(fbp.willshow[i].exists()){
								if(delete(fbp.willshow[i])){
									try{
										if(mContentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,MediaStore.MediaColumns.DATA+" = "+"?",new String[]{path})<0){
											undeleted++;
										}
									}catch(Exception e){log.alert(e.toString());}
								}else undeleted++;
							}else{
								try{
									if(mContentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,MediaStore.MediaColumns.DATA+" = "+"?",new String[]{path})<0){
										undeleted++;
									}
								}catch(Exception e){log.alert(e.toString());}
							}
						}
					}
					if(undeleted>0){
						log.alert("出错",undeleted+"个项目删除失败");
					}else{
						Toast.makeText(ma,"删除成功",0).show();
					}
					ArrayList<File> fs=new ArrayList<File>();
					try{
						c = mContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
						int indexData = c.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
						while(c.moveToNext()){
							String path = c.getString(indexData);
							File tmp=new File(path);
							fs.add(tmp);
						}
					}catch(Exception e){log.alert(e.toString());}
					File[] files=new File[fs.size()];
					fs.toArray(files);
					fbp.files=files;
					fbp.setWillShow(files);
					dismiss();
				}else{
					for(int i=0;i<fbp.selected.length;i++){
						if(fbp.selected[i]){
							delete(fbp.willshow[i]);
						}
					}
					if(undeleted>0){
						log.alert("出错",undeleted+"个项目删除失败");
					}else{
						Toast.makeText(ma,"删除成功",0).show();
					}
					dismiss();
					fbp.setWillShow(fbp.currentDirectory.listFiles());
				}
			}else{
				if(delete(destination)){
					Toast.makeText(ma,"成功",0).show();
					dismiss();
					if(fbp.dyn==10||fbp.dyn==11){
						ArrayList<File> fs=new ArrayList<File>();
						try{
							ContentResolver mContentResolver=ma.getContentResolver();
							Cursor c = mContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
							int indexData = c.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
							while(c.moveToNext()){
								String path = c.getString(indexData);
								File tmp=new File(path);
								if(destination.equals(tmp)){//file was renamed
									//MediaStore.createWriteRequest(
									Toast.makeText(ma,"更新媒体存储",0).show();
									if(mContentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,MediaStore.MediaColumns.DATA+" = "+"?",new String[]{path})<0){
										log.alert("更新媒体存储失败");
									}
								}else{
									fs.add(tmp);
								}
							}
						}catch(Exception e){
							log.alert(e.toString());
						}
						File[] files=new File[fs.size()];
						fs.toArray(files);
						fbp.files=files;
						fbp.setWillShow(files);
					}else fbp.setWillShow(fbp.currentDirectory.listFiles());
				}else if(fbp.dyn==10||fbp.dyn==11){
					ArrayList<File> fs=new ArrayList<File>();
					try{
						ContentResolver mContentResolver=ma.getContentResolver();
						Cursor c = mContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
						int indexData = c.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
						while(c.moveToNext()){
							String path = c.getString(indexData);
							File tmp=new File(path);
							if(destination.equals(tmp)){//file was renamed
								if(mContentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,MediaStore.MediaColumns.DATA+" = "+"?",new String[]{path})<0){
									log.alert("更新媒体存储失败");
									break;
								}else{
									while(c.moveToNext()){
										path = c.getString(indexData);
										tmp=new File(path);
										fs.add(tmp);
									}
									File[] files=new File[fs.size()];
									fs.toArray(files);
									fbp.files=files;
									fbp.setWillShow(files);
									break;
								}
							}else{
								fs.add(tmp);
							}
						}
						dismiss();
					}catch(Exception e){
						log.alert(e.toString());
					}
				}else{
					log.alert("异常");
					dismiss();
				}
			}
		}
	}
	int undeleted=0;
	boolean delete(File f){
		if(f.isDirectory()){
			if(!f.canRead()){
				return false;
			}
			File[] ls=f.listFiles();
			for(int index=0;index<ls.length;index++){
				delete(ls[index]);
			}
			try{
				if(f.delete()){
					return true;
				}else{
					undeleted++;
					return false;
				}
			}catch(Exception e){}
		}else{
			try{
				if(f.delete()){
					return true;
				}else{
					undeleted++;
					return false;
				}
			}catch(Exception e){
			}
		}
		undeleted++;
		return false;
	}
	@Override
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		Paint p=new Paint();
		p.setColor(fbp.borderColor);
		canvas.drawRect(0,0,fbp.borderWidth,getHeight(),p);
		canvas.drawRect(0,0,getWidth(),fbp.borderWidth,p);
		canvas.drawRect(getWidth()-fbp.borderWidth,0,getWidth(),getHeight(),p);
		canvas.drawRect(0,getHeight()-fbp.borderWidth,getWidth(),getHeight(),p);
	}
}
